<?php
// @formatter:off
/**
 * @file WCUsersHolder.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-11 21:02:48 +0000 (Thu, 11 Jul 2013) $
 *
 * $Id: WCUsersHolder.php 95 2013-07-11 21:02:48Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/includes/holders/WCUsersHolder.php $
 */
// @formatter:on

require_once "{$wcPaths["includes-directory"]}/representations/WCUser.php";

/**
 * @class WCComicsHolder This class is a holder for representations of table
 * '%users', in other words, a holder for WCUser objects.
 */
class WCUsersHolder extends WCBasicHolder {
	/**
	 * @var WCUsersHolder Pointer to the singleton instance.
	 */
	protected static $_Instance;
	/**
	 * @var string Class name of the objects to be held.
	 */
	protected $_PropertiesItemClass = "WCUser";
	/**
	 * @var string This is the prefix of every column on the table
	 * '%users'.
	 */
	protected $_PropertiesPrefix = "usr_";
	/**
	 * @var string This is the name of the table represented by WCUser
	 * objects. In this case, '%users'.
	 */
	protected $_PropertiesTable = "users";
	/**
	 * @var string This property indicates that the column name is not the
	 * default value. This means it is 'usr_username' instead of 'usr_name'.
	 */
	protected $_PropertiesNameColumn = "username";
	//
	// Public methods.
	/**
	 * This method allows to create a new item in the database. It also adds
	 * it to the internal list.
	 *
	 * @param string $name Name of the new item to be created.
	 * @return WCTableRepresentation On success returns the new object.
	 * Otherwise returns a null pointer.
	 */
	public function create($name) {
		//
		// Calling for parent class item creator.
		$user = parent::create($name);
		//
		// If the new item was successfully created it is updated with
		// more required data.
		//
		// @note when Read Only Mode is activated, parent method create()
		// will return NULL, therefore, it's no needed more checks for
		// such mode.
		if($user != null) {
			//
			// Creating a random hash code to use as flag for user
			// activation.
			$hash = "{$user->id()}|".md5(rand(1, 1000) * rand(1, 1000));
			//
			// Creating a statement to set the user as
			// "awaiting activation".
			// @{
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "update  {$this->_dbprefix}users\n";
				$query .= "set     usr_hash_register = :hash,\n";
				$query .= "        usr_hash_dregister = now()\n";
				$query .= "where   usr_id = :id";
				$stmt = $this->_db->prepare($stmtId, $query);
			}
			// @}
			//
			// Building database parameters.
			$params = array(
				":id" => $user->id(),
				":hash" => $hash
			);
			//
			// Updating user.
			$stmt->execute($params);
			//
			// Reloading represented object to avoid issues.
			$user->reload();
		}
		//
		// Returning new item.
		return $user;
	}
	/**
	 * This method allows to get a user based on it's email address.
	 * 
	 * @param string email E-Mail to search.
	 * @return WCUser On success returns a user. Otherwise, returns null.
	 */
	public function userByEmail($email) {
		// 
		// Setting a default value to be returned.
		$out = null;
		//
		// Creating a statement to get users matching an email address.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  usd_id\n";
			$query .= "from    {$this->_dbprefix}user_data\n";
			$query .= "where   usd_email = :email";
			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		// 
		// Creating a structure to request database data.
		$params = array(":email" => $email);
		// 
		// Searching user.
		if($stmt->execute($params) && $stmt->rowCount() == 1) {
			// 
			// At this point, user was found.
			$row = $stmt->fetch();
			// 
			// Getting the user by its ID.
			$out = $this->item($row["usd_id"]);
		}
		// 
		// Returning found user.
		return $out;
	}
	//
	// Public class methods.
	/**
	 * This class method allows to get the sole instance of this holder.
	 *
	 * @return WCUsersHolder Returns the singleton instance of this
	 * class.
	 */
	public static function Instance() {
		//
		// Checking if there's no instance yet.
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			//
			// Creating the sole instance.
			self::$_Instance = new $c();
		}
		//
		// Returning the class instance.
		return self::$_Instance;
	}
}
?>